//
//  SRHistoryDataSR03.m
//  sr01sdkProject
//
//  Created by Linktop on 2022/8/12.
//

#import "SRHistoryDataSR03.h"

const uint32_t DATA_INVALID_UUID = 0xFFFFFF;  // 无效数据的uuid

const BOOL USE_HEART_FIX = YES;

@implementation SRHistoryDataSR03

- (instancetype)initWithData:(NSData *)data
{
    if (self = [super init]) {
        
        if (data.length < 20) {
            return self;
        }
//        DebugNSLog(@"历史 原始data %@", data);
        uint8_t *dataBytes = (uint8_t *)[data bytes];
        
        
        // 四字节时间戳
        uint32_t timeStamp = *((uint32_t *)(dataBytes + 2));//(dataBytes[2] << 24) +  (dataBytes[3] << 16) + (dataBytes[4] << 8) + dataBytes[5];
//        DebugNSLog(@"timeStamp = 0X%x timeStamp2 = 0X%x", timeStamp, timeStamp2);

        _timeStamp = @(timeStamp);
        
        UInt8 status = dataBytes[6];  // 状态
       
        
        // index = 7 设备处理后的心率
        UInt8 deviceModifyHeartRate = dataBytes[7];  // 设备处理后的心率
        if (deviceModifyHeartRate == 0xFF) {
            deviceModifyHeartRate = 0;
        }
        _deviceModifyHeartRate = @(deviceModifyHeartRate);
        
        // 8 运动检测技术 低位
        UInt8 sport_lowbit = dataBytes[8];  // 运动检测技术 低位
        
        SR03HIS_MEASURE_TYPE type = [self analyStatus:status SportLowBit:sport_lowbit IsShortMition:dataBytes[1] == 0x91 || dataBytes[1] == 0x92];
        if (type == SR03HIS_MEASURE_TYPE_HR) {
            _heart_ori_3 = @(dataBytes[15]);
        } else {
            int oxyTemp = dataBytes[15] & 0xFF;
            if (oxyTemp > 60 && oxyTemp <= 100) {
                _bloodOxygen = @(dataBytes[15]);
            }
        }
        
        // 9 10 11 uuid 数据的index
        uint32_t uuid = (dataBytes[11]<< 16) +(dataBytes[10]<< 8) +dataBytes[9];
        _dataUuid = @(uuid);
        
        if (dataBytes[13] >> 4 == 0x0E) {
            // 13 + 14 为hrv  15 为呼吸率 sr09新增协议
            _hrv = @( ((dataBytes[13] & 0x0F)<<8) + dataBytes[14] );
            if (type == SR03HIS_MEASURE_TYPE_HR) {
                _breathRate = @(dataBytes[15] & 0xFF); //呼吸率
            } else {
                _breathRate = @(deviceModifyHeartRate/4);
            }
            
        } else {
            // sr03 逻辑
            // 13 原始心率 1
            _heart_ori_1 = @(dataBytes[13]);
            // 14 原始心率 21
            _heart_ori_2 = @(dataBytes[14]);
        }
    
        
        // 15 体温
        float skinTemp = (dataBytes[16] + 200) / 10.0f; // 体表温度
        _skinTemp = @(skinTemp);
        
        // 16 17 步数
        uint16_t steps = (dataBytes[18] << 8) + dataBytes[17]; // 步数
        _steps = @(steps);

        if (dataBytes[1] == 0X92) { // DEVICE_RESPONSE_HISTORY_DATA_3
            _lis2ds12 = YES;

        }
        
        if(dataBytes[1] == 0x91 || dataBytes[1] == 0x92) {
            _battery = @(dataBytes[12] & 0x7F); // 1.1.7 添加支持
        }

    }
    return self;
}

-(SR03HIS_MEASURE_TYPE)analyStatus:(uint8_t)statusByte SportLowBit:(uint8_t)sportLowBit IsShortMition:(BOOL)isshortMotion
{
//    DebugNSLog(@"历史 状态 %X", statusByte);
    // sos
    BOOL isSos = ((statusByte >> 7) & 0X01) == 1;
    _isSos = isSos;
    _isCharging = _isSos;
    
    // 是否穿戴
    BOOL isWear = ((statusByte >> 6) & 0X01) == 1;
    _isWear = @(isWear);
    
    uint8_t soprt_higt_bit = statusByte & 0x1F;
    
    if (isshortMotion) { // 新协议 空一位 给运动模式标记
        soprt_higt_bit = statusByte & 0x0F;
        _isSportMode =  @((statusByte >> 4) & 0x01);
        if (_isSportMode.intValue) {
            DebugNSLog(@"历史包 运动模式");
        }
    }
    
    _soprtTimes = @( (soprt_higt_bit << 8) + sportLowBit );
    
    uint8_t measureMode = ((statusByte >> 5) & 0X01);
    if (measureMode == 0) {
        _measureType = SR03HIS_MEASURE_TYPE_HR;
    } else {
        _measureType = SR03HIS_MEASURE_TYPE_OXYGEN;

    }
    
    return _measureType;
    //
//    NSUInteger soprtTimes = statusByte & 0X3F;  // 运动触发次数
//    _soprtTimes = @(soprtTimes);
}

@end
